﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.Domain.Aggregates.Deals;
using NCP.Recruiting.Domain.Aggregates.Contracts;
using NCP.Recruiting.ApplicationServices.Messaging.Sales;
using NCP.Recruiting.ApplicationServices.ViewModels;
using NCP.Infrastructure.Common;

namespace NCP.Recruiting.ApplicationServices.Implementations {
    public class SalesAppService : ISalesAppService {

        private readonly IDealRepository _dealRepository;
        private readonly IContractRepository _contractRepository;

        public SalesAppService(IDealRepository dealRepository, IContractRepository contractRepository) {
            this._contractRepository = contractRepository;
            this._dealRepository = dealRepository;
        }

        GetContractsByCompanyResponse ISalesAppService.GetContractsByCompany(GetContractsByCompanyRequest request) {
            var contracts = _contractRepository.GetPagedList(request.PageIndex,
                request.PageCount,
                ContractSpecifications.GetContractsByCompany(request.Id),
                c => c.StartDate,
                false);
            var vmcontracts = contracts.Select(
                x => new ContractViewModel {
                    CompanyId = x.CompanyId,
                    ContractNumber = x.ContractNumber,
                    EndDate = x.EndDate,
                    Id = x.Id,
                    MemberLevel = x.MemberLevel,
                    Positions = x.Positions,
                    Resumes = x.Resumes,
                    StartDate = x.StartDate,
                    Status = x.Status,
                    Title = x.Title
                });

            var response = new GetContractsByCompanyResponse {
                PagedContracts = new PagedList<ContractViewModel>(vmcontracts, contracts.PageIndex, contracts.PageSize, contracts.TotalCount)
            };       

            return response;
        }

        GetDealsByCompanyResponse ISalesAppService.GetDealsByCompany(GetDealsByCompanyRequest request) {
            var deals = _dealRepository.GetPagedList(request.PageIndex,
                request.PageCount,
                DealSpecifications.GetDealsByCompany(request.Id),
                d => d.DealOn,
                false);

            var vmdeals = deals.Select(
                x => new DealViewModel {
                    DealNumber = x.DealNumber,
                    DealOn = x.DealOn,
                    DealSummary = x.DealSummary,
                    Deleted = x.Deleted,
                    Direction = x.Direction,
                    ExpiresOn = x.ExpiresOn,
                    Points = x.Points,
                    Remark = x.Remark,
                    Source = x.Source,
                    Id = x.Id
                });

            GetDealsByCompanyResponse response = new GetDealsByCompanyResponse();
            response.PagedDeals = new PagedList<DealViewModel>(vmdeals, deals.PageIndex, deals.PageSize, deals.TotalCount);

            return response;
        }
    }
}
